package edu.webtech.project.client.view;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FileUpload;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteEvent;
import com.google.gwt.user.client.ui.FormPanel.SubmitEvent;
import com.google.gwt.user.client.ui.Hidden;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

import edu.webtech.project.client.FileService;
import edu.webtech.project.client.FileServiceAsync;

public class FileUploadPanel extends Composite {

	private static FileUploadPanelUiBinder uiBinder = GWT
			.create(FileUploadPanelUiBinder.class);
	
	FileServiceAsync fileService = GWT.create(FileService.class);
	
	@UiField VerticalPanel fileUploadPanel;
	@UiField FileUpload txtUploadBox;
	@UiField Button btnUpload;
	@UiField ListBox listSemesterBox;
	@UiField ListBox listCourseName;
	@UiField FormPanel formUploadFile;
	@UiField Label error;
	@UiField Hidden fileName;

	interface FileUploadPanelUiBinder extends UiBinder<Widget, FileUploadPanel> {
	}

	public FileUploadPanel() {
		initWidget(uiBinder.createAndBindUi(this));
		
		// Some important initializations
		txtUploadBox.setName("upload");
		formUploadFile.setEncoding(FormPanel.ENCODING_MULTIPART);
		formUploadFile.setMethod(FormPanel.METHOD_POST);
		
		populateSemesterName();
		populateCourseName();
		
	}
	
	public void populateSemesterName() {
		String[] names = { "Select semester", "SS2009", "WS2009", "SS2010",
				"WS2010", "SS2011", "WS2011", "SS2012", "WS2012", "SS2013",
				"WS2013" };
		for (String name : names) {
			listSemesterBox.addItem(name);
		}

	}

	public void populateCourseName() {
		String[] names = { "Select course", "Medicine", "Informatics",
				"Electrical Eng.", "Communication Eng." };
		for (String name : names) {
			listCourseName.addItem(name);
		}
	}
	
	@UiHandler("btnUpload")
	void onButtonUpload(ClickEvent e) {
		
		error.setText("");
		if (txtUploadBox.getFilename().isEmpty()) {
			error.setText("Please enter a file name.");
			return;
		}
		
		String strSemester = listSemesterBox.getItemText(listSemesterBox.getSelectedIndex());
		String strCourse   = listCourseName.getItemText(listCourseName.getSelectedIndex());
		
		if (strSemester.compareToIgnoreCase("select semester") == 0 ||
				strCourse.compareToIgnoreCase("select course") == 0) {
			error.setText("Please mention both semester and course");
			return;
		}
		
		String strFullFileName = txtUploadBox.getFilename();
		int index = strFullFileName.lastIndexOf("\\");
		
		if (index != -1) {			
			strFullFileName = strFullFileName.substring(index + 1);
		}
		
		if (strFullFileName.length() > 15) {

			String strExt = "";
			index = strFullFileName.lastIndexOf(".");
			if (index != -1) {
				strExt = strFullFileName.substring(index);
			}

			String strFileName = strFullFileName.substring(0, 10) + strExt;
			strFileName = strFileName.trim();
			fileName.setDefaultValue(strFileName);

		} else {
			fileName.setDefaultValue(strFullFileName);
		}
		
		fileService.getFileUploadURL(new AsyncCallback<String>() {

			@Override
			public void onFailure(Throwable caught) {
				Window.alert("OMG !! What shall I do now?");
			}

			@Override
			public void onSuccess(String result) {
				
				System.out.println(result);
				formUploadFile.setAction(result);
				formUploadFile.submit();				
			}
		});
		
		
	}
	
	@UiHandler("formUploadFile")
	void onSubmitForm(SubmitCompleteEvent e) {
		Window.alert("Form submitted successfully.");
		formUploadFile.reset();
		
	}
	
	@UiHandler("formUploadFile")
	void onSubmitForm(SubmitEvent e) {
		 // Window.alert("Hello World");
	}

}
